
//console.log(montharr)

new Vue({
    el:'#main',
    data:()=>({
        search:'',
        montharr:[],
        item:0,//产品or配件
        year:'',//年
        month:'',//月
        listdata:[],
        batch:[],
        allche:[],
        page:1,
        pageSize:10
    }),
    methods:{
        getdata(){
            // console.log(this.search)
            $.ajax({
                url:'/admin/buy/plan',
                type:'post',
                data:{
                   item:this.item,
                   year:this.year,
                   month:this.month,
                   searchText:this.search,
                   //page:this.page,
                   //limit:2
                },
                success:(data)=>{
                    data = JSON.parse(data)
                    $('#allche').prop('checked',false)
                    this.listdata=data.data
                    this.listdata.forEach(element => {
                        this.allche.push(element.id)
                    });
                    /* if(data.size / 1>=1){
                        $('#pagination').show()
                        this.Resetnav(this.page,data.size)
                    }else{
                        $('#pagination').hide()
                    } */
                }
            })
        },
        changeitem(item){
            this.batch=[]
            this.page=1
            this.item=item
            this.getdata()
        },
        changemonth(year,month){
            this.batch=[]
            this.page=1
            this.month=month,
            this.year=year
            this.getdata()
        },
        allsel(event){
            if($(event.target).prop('checked')){
                $(event.target).parents('table').find('input').prop('checked',true)
                this.batch=this.allche
            }else{
                $(event.target).parents('table').find('input').prop('checked',false)
                this.batch=[]
            }
            console.log(this.batch)
        },
        onesel(did){
            if(this.batch.indexOf(did)==-1){
                this.batch.push(did)
            }else{
                this.batch.splice(this.batch.indexOf(did),1)
            }
            var isall=true
            $('td input').each(function() {
                if(!$(this).prop('checked')){
                    isall=false
                }
            })
            $('th input').prop('checked',isall)
        },
        delfun(id){
            if((typeof id=='object')&&id.constructor==Array){
                var data={ids:id}
            }else{
                var data={ids:[id]}
            }
            $.ajax({
                url:'/admin/buy/buy_planDel',
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if(data.status==1){
                        this.getdata()
                    }
                    this.$refs.alert.style.display='block'
                    this.$refs.info.innerHTML=data.msg
                }
            }) 
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+parseInt(countindex)+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        banckfun(){
            $.ajax({
                url:'/admin/buyapi/hasterms',
                type:"post",
                success:data=>{
                    change_mes_type('.alert',data.status)
                    if(this.batch.length && data.status==1){
                        location.href='/admin/buy/plan2po?plan_ids='+this.batch.join(',')
                    }
                    else{
                        this.$refs.alert.style.display='block'
                        this.$refs.info.innerHTML=this.$refs.waner.value
                    }
                }
            })
            
        }
    },
    mounted(){
        var year=new Date().getFullYear()
        var current=new Date().getMonth()+1
        this.year=year
        this.month=current
        this.montharr.push({year:year,month:current})
        for(var i=1;i<=6;i++){
            current++
            if(current>12){
                year++
                current=1
            }
            this.montharr.push({year:year,month:current})
        }
        this.getdata()
    }
})